import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import UserManageView from '../views/UserManageView.vue'
import MessageTemplateView from '../views/MessageTemplateView.vue'
import AutoReplyManageView from '../views/AutoReplyManageView.vue'
import ProfileView from '../views/ProfileView.vue'
import WechatMenuView from '../views/WechatMenuView.vue'
import SendWechatMessage from '../views/SendWechatMessage.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView
    },
    {
      path: '/user',
      name: 'user',
      component: UserManageView
    },
    {
      path: '/message-template',
      name: 'message-template',
      component: MessageTemplateView
    },
    {
      path: '/auto-reply',
      name: 'auto-reply',
      component: AutoReplyManageView
    },
    {
      path: '/profile',
      name: 'profile',
      component: ProfileView
    },
    {
      path: '/wechat-menu',
      name: 'wechat-menu',
      component: WechatMenuView
    },
    {
      path: '/send-wechat-message',
      name: 'send-wechat-message',
      component: SendWechatMessage
    }
  ]
})

// 添加导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
